﻿package application
{
	import exey.comps.text.TextFieldLabel;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import net.hires.debug.Stats;
	import render3d.adapters.Alternativa3DAdapter;
	import render3d.adapters.Away3DAdapter;
	import render3d.adapters.Papervision3DAdapter;
	import render3d.adapters.Sandy3DAdapter;
	import render3d.interfaces.IRender3DAdapter;
	import shooter.Game;
	
	/**
	 * Ratburner3D Main Class
	 * @author Exey Panteleev
	 */
	[ SWF( backgroundColor = '#467cbd', width = '800', height = '600', frameRate='60' ) ]
	public class Main extends Sprite
	{
		private var renderAlternativa3D:Alternativa3DAdapter;
		private var renderPapervision3D:Papervision3DAdapter;
		private var renderAway3D:Away3DAdapter;
		private var renderSandy3D:Sandy3DAdapter;
		
		public var renderLabel:Bitmap;
		public var renderCopyright:TextField;
		
		public var game:Game;
		public var render:IRender3DAdapter;
		
		private var stats:Stats = new Stats( { bg: 0x202020, fps: 0xFCFCFC, ms: 0x00ff00, mem: 0xCCCC00, memmax: 0xff0070 } );
		private var mainMenu:MainMenu;
		private var renderContainer:Sprite;
		private var mainMenuContainer:Sprite;

		public function Main()
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init(e:Event = null):void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.quality = StageQuality.LOW;
			
			renderContainer = new Sprite()
			addChild(renderContainer)
			
			mainMenuContainer = new Sprite()
			addChild(mainMenuContainer)
			addMainMenu()
			
			// FPS Counter
			stats.alpha = .9;
			addChild( stats ); // Mr.Doob Stats http://code.google.com/p/mrdoob/
			
			renderCopyright = new TextFieldLabel(this, 500, 780, new TextFormat("Verdana", 9, 0x000000));
		}
		
		private function addMainMenu():void 
		{
			mainMenu = new MainMenu();
			mainMenu.addEventListener(MainMenuEvent.PLAY_CLICK, onClickPlay);
			mainMenuContainer.addChild(mainMenu);
		}
		
		private function onClickPlay(e:MainMenuEvent):void 
		{
			removeChild(mainMenuContainer);
			initGame(e.renderName, e.tmxUrl);
			addListeners();
			onResize();
			renderLabel.visible = true;
			renderCopyright.visible = true;
		}
		
		private function addListeners():void
		{
			game.addEventListener("goToMainMenu", onGoToMainMenu);
			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(Event.RESIZE, onResize);
		}
		
		private function onGoToMainMenu(e:Event):void 
		{
			game.destroy()
			renderContainer.removeChild(Object(render).viewContainer);
			addChild(mainMenuContainer);
			renderLabel.visible = false;
			renderCopyright.visible = false;
		}
		
		private function initGame(renderName:String, tmxUrl:String = ""):void 
		{
			initRender(renderName)
			game = new Game(this, render, tmxUrl);
		}

		public function initRender(renderName:String):void 
		{
			if (renderName == MainMenu.ALTERNATIVA3D)
			{
				renderAlternativa3D = new Alternativa3DAdapter();
				renderContainer.addChild(renderAlternativa3D.viewContainer);
				render = renderAlternativa3D;
			}
			else if (renderName == MainMenu.AWAY3D)
			{
				renderAway3D = new Away3DAdapter();
				renderContainer.addChild(renderAway3D.viewContainer);
				render = renderAway3D;
			}
			else if (renderName == MainMenu.PAPERVISION3D)
			{ 
				renderPapervision3D = new Papervision3DAdapter();
				renderContainer.addChild(renderPapervision3D.viewContainer);
				render = renderPapervision3D;
			}
			else if (renderName == MainMenu.SANDY3D)
			{
				renderSandy3D = new Sandy3DAdapter();
				renderContainer.addChild(renderSandy3D.viewContainer);
				render = renderSandy3D;
			}
			changeLabel(renderName);
			renderCopyright.text = "Powered by " + renderName;
		}
		
		public function changeLabel(renderName:String):void 
		{
			if(renderLabel) removeChild(renderLabel)
			if (renderName == MainMenu.ALTERNATIVA3D) 
				renderLabel = new Bitmap(MainMenu.LABEL_ALTERNATIVA3D_BITMAP_DATA);
			else if (renderName == MainMenu.AWAY3D) 
				renderLabel = new Bitmap(MainMenu.LABEL_AWAY3D_BITMAP_DATA);
			else if (renderName == MainMenu.PAPERVISION3D) 
				renderLabel = new Bitmap(MainMenu.LABEL_PAPERVISION3D_BITMAP_DATA);				
			else if (renderName == MainMenu.SANDY3D) 
				renderLabel = new Bitmap(MainMenu.LABEL_SANDY3D_BITMAP_DATA);
			addChild(renderLabel);
		}
		
		private function onEnterFrame(e:Event):void 
		{
			game.update();
			render.update();
		}
		
		private function onResize(e:Event = null):void 
		{
			game.resize(stage.stageWidth, stage.stageHeight);
			render.resize(stage.stageWidth, stage.stageHeight);
			renderLabel.x = stage.stageWidth - renderLabel.width - 10;
			renderLabel.y = stage.stageHeight - renderLabel.height - 20;
			renderCopyright.x = stage.stageWidth - renderCopyright.width - 10;
			renderCopyright.y = stage.stageHeight - renderCopyright.height - 2;
		}
		
	}
	
}